define erp='erp_mli2.';
define outils='outils_mli2.';

--create table se_PAR_MODPAIECHCALC as (select * from &erp.par_modpaiechcalc where 1=2);


DELETE FROM  se_PAR_MODPAIECHCALC ;



INSERT INTO se_PAR_MODPAIECHCALC

SELECT  id modpaiechcalc_cod,
        0 enreg_id,
        'N' enreg_s,
        libelle modpaiechcalc_lib,
        NULL modpaiechcalc_lib2,
        Decode(jfm,30,31,jfm) fdm_jour,
        jcre credit_jour,
        jval valeur_jour
FROM
 ( SELECT p.*,mp.modpai,mr.modreg,po.pourc
   FROM uv_cpaiements p, uv_cpaiements_modpai  mp, uv_cpaiements_modreg mr, uv_cpaiements_pourc po
   WHERE p.id=mp.id(+)
     AND mr.id(+)=mp.id AND mr.RANGMULTI2(+) =mp.RANGMULTI2
     AND po.id(+)=mp.id AND po.RANGMULTI2(+) =mp.RANGMULTI2
     AND mp.modpai IS null
 --ORDER BY p.id
) ;

 UPDATE se_PAR_MODPAIECHCALC tf
 SET enreg_id = (select enreg_id FROM &erp.PAR_MODPAIECHCALC f WHERE f.modpaiechcalc_cod=tf.modpaiechcalc_cod )
 WHERE EXISTS (SELECT 1 FROM &erp.PAR_MODPAIECHCALC f WHERE f.modpaiechcalc_cod=tf.modpaiechcalc_cod);

 UPDATE se_PAR_MODPAIECHCALC SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL OR enreg_id =0;

 CALL uvse_synchroverserp('modpaiechcalc_cod','PAR_MODPAIECHCALC','O') ;



 commit;

select * from (
SELECT &outils.stragg(source) source, modpaiechcalc_cod, enreg_id, enreg_s, modpaiechcalc_lib, modpaiechcalc_lib2, fdm_jour, credit_jour, valeur_jour
FROM (
  SELECT 'uv' source ,g.* FROM se_PAR_MODPAIECHCALC g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.PAR_MODPAIECHCALC g
  )
GROUP BY modpaiechcalc_cod, enreg_id, enreg_s, modpaiechcalc_lib, modpaiechcalc_lib2, fdm_jour, credit_jour, valeur_jour
HAVING Count(1)!=2) r
ORDER BY modpaiechcalc_cod;